package fivecardstudd;

import java.util.*;
public class Player  {
 Scanner input=new Scanner(System.in);  
 
    public enum RoundStatus {
            INACTIVE, WAITING_FOR_TURN, IN_TURN, JUST_BET, WON;
    }
    
    
    private String name;
    private int chips;
    private int lastBet;
    private Table table; 
    private Hand currentHand;
    private RoundStatus roundStatus;

    public Player( int chips, Table table){
            
            this.chips = chips;
            lastBet = 0;
            this.table = table;
            currentHand = new Hand();
            roundStatus = RoundStatus.INACTIVE; 
            
    }
    

    public void getPlayerMove(){
     
     			System.out.println(getName()+",Do you want to call (c), raise(r), fold(f), or all in (a)");
     			String s=input.next();
     		if(s.startsWith("c")){
     			call();
     		}
     		else if (s.startsWith("r")){
     			System.out.println("Raise how much ?");
     			int amount=input.nextInt();
     			raise(amount);
     		}
     		else if(s.startsWith("a")){
     			allIn();
     		}
     		else if(s.startsWith("f")){
     			fold();
     		}
     		
     	}
     
    
    
    
    
    
    
    
    
    public String getName() {
            return name;
    }

    public void setName(String name) {
            this.name = name;
    }

    public int getChips() {
            return chips;
    }

    public void setChips(int chips) {
            this.chips = chips;
    }

    public int getLastBet() {
            return lastBet;
    }
    
    public void resetLastBet(){
            lastBet = 0;
    }

    public Hand getCurrentHand() {
            return currentHand;
    }

    public void setCurrentHand(Hand currentHand) {
            this.currentHand = currentHand;
    }

    public RoundStatus getRoundStatus() {
            return roundStatus;
    }

    public void setRoundStatus(RoundStatus roundStatus) {
            this.roundStatus = roundStatus;
    }
    
   
    public int getValueOfCard(int n) {
            return currentHand.getHand().get(n).asInt();
    }
    
  
    public void handReset(){
            currentHand.reset();
    }
    
  
    public void setActive(){
            this.setRoundStatus(RoundStatus.WAITING_FOR_TURN);
    }
    

    public void setTurn(){
            this.setRoundStatus(RoundStatus.IN_TURN);
    }
    
   
    public boolean inTurn(){
            return this.getRoundStatus() == RoundStatus.IN_TURN;
    }
    
  
    public boolean getActive(){
            return this.getRoundStatus() != RoundStatus.INACTIVE;
    }
    
    
    public boolean hasBet(){
            return this.getRoundStatus() == RoundStatus.JUST_BET;
    }
    
  
    public void addCardToHand(Card c) {
            currentHand.addCard(c);
    }
    
   
    public void bet(int betAmount){
            chips = chips - betAmount;
            table.increasePot(betAmount);
            lastBet += betAmount;

         
            System.out.println(this+", you just bet "+betAmount+" you have "+chips+" chips left");
    }
    

    public void call(){
            if(table.getCurrentBet()-lastBet >= chips){
                    bet(chips);
            }
            else{
                    bet(table.getCurrentBet()-lastBet);
            }
            
            setRoundStatus(RoundStatus.JUST_BET);
    }
    
   
    public void raise(int bet){
            table.setCurrentBet(table.getCurrentBet()+bet);
            call();
    }
    

    public void allIn(){
            table.setCurrentBet(chips);
            bet(chips);
            setRoundStatus(RoundStatus.JUST_BET);
    }
    

    public void fold(){
            this.setRoundStatus(RoundStatus.INACTIVE);
    }
    

    public void wonHand(int winnings){
          
            chips += winnings;
            setRoundStatus(RoundStatus.WON);
    }
    
  

    @Override
    public String toString() {
            return name;
    }
}